@inject IUserStatusService statusService
@inject IRescanService rescanService
@inject ViewDataService ViewDataService
@inject NavigationManager navManager

<div class="title-bar">
    <div class="title-section">
        <NavBack/>
        <MudButton @onclick="@ClearFaceThumbs">Refresh Thumbs</MudButton>
    </div>
    <div class="title-section">
        <MudRadioGroup @bind-Value="filterType">
            @foreach ( var choice in Enum.GetValues<FilterType>() )
            {
                <MudRadio Value="@choice" Color="Color.Primary">
                    @choice.ToString()
                </MudRadio>
            }
        </MudRadioGroup>
    </div>
    <div class="title-section">
        <ConflatedTextBox IconClass="fa-search" id="searchBox" placeholder="Search" OnValueChanged="@DoSearch" TextValue="@searchText"/>
    </div>
</div>
<div class="damselfly-tagview">
    <PeopleGrid SearchText="@searchText" PersonState="@searchState" OnPersonClicked="PersonClicked"/>
</div>

@code {

    [Parameter] public string PersonName { get; set; }

    string searchText;

    Person.PersonState? searchState => filterType switch
    {
        FilterType.Identified => Person.PersonState.Identified,
        FilterType.Unidentified => Person.PersonState.Unknown,
        _ => null
    };

    FilterType filterType = FilterType.All;

    private enum FilterType
    {
        All,
        Identified,
        Unidentified
    }

    private void PersonClicked( Person person )
    {
        if( person != null)
            navManager.NavigateTo($"/?personid={person.PersonId}");
    }
    private void DoSearch(string searchTerm)
    {
        if( searchText != searchTerm )
        {
            statusService.UpdateStatus($"Searching for {searchTerm}...");
            searchText = searchTerm;
            StateHasChanged();
        }
    }

    private void ClearFaceThumbs()
    {
        statusService.UpdateStatus("Deleting face thumbnails...");

        _ = rescanService.ClearFaceThumbs();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if( firstRender )
            ViewDataService.SetSideBarState(new ViewDataService.SideBarState { ShowBasket = true, ShowTags = true });

        await base.OnAfterRenderAsync(firstRender);
    }

}